package net.aynt.blog.action.admin;

import java.io.File;
import java.io.IOException;

import net.aynt.core.mvc.WebContext;
//import net.aynt.core.util.web.ImgStorageService;
import net.aynt.core.util.web.QiNiuStorageService;

public class FileAction {
	private static final long MAX_IMG_SIZE = 1 * 1024 * 1024;

	public void upload(WebContext rc) throws IOException {
		try {
			File imgFile = rc.getImage("imgFile");
			if (imgFile.length() > MAX_IMG_SIZE) {
				rc.printJson(new String[] { "error", "message" }, new Object[] {
						1, "File is too large" });
				return;
			}
//			StorageService ss = StorageService.IMAGE;   存储在项目
//			ImgStorageService ss = ImgStorageService.IMAGE; 存储在自定义域名
			QiNiuStorageService ss = QiNiuStorageService.IMAGE;//存储在七牛
			String path = ss.save(imgFile);
			String url = ss.getReadPath() + path;
			rc.printJson(new String[] { "error", "url" },
					new Object[] { 0, url });
		} catch (Exception e) {
			rc.printJson(new String[] {"error", "message"}, new Object[] {
					1, "图片上传出错！"});
		}
	}
}
